/**
 * Copyright (C) 2009, 2010 SC 4ViewSoft SRL
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cc.mango.android.chart;

import java.util.ArrayList;
import java.util.List;

import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;

import cc.telecomdigital.tdstock.activity.chart.NvChart;

/**
 * The XY chart rendering class.
 */
public abstract class DownXYChart extends BaseXYChart
{

	/**
	 * Builds a new XY chart instance.
	 * 
	 * @param dataset the multiple series dataset
	 * @param renderer the multiple series renderer
	 */
	public DownXYChart(XYMultipleSeriesDataset dataset,
			XYMultipleSeriesRenderer renderer)
	{
		super(dataset, renderer);
	}

	public DownXYChart(XYMultipleSeriesDataset dataset,
			XYMultipleSeriesRenderer renderer, NvChart nvChart)
	{
		// TODO Auto-generated constructor stub
		super(dataset, renderer, nvChart);
	}

	protected String getStringYLabel(double vol)
	{

		int k = 1000;
		int m = 1000000;
		int b = 1000000000;
		//	  double t = 1000000000000;

		long l = 0;
		if (vol / k >= b)
		{

			l = Math.round(vol / b / k);
			return l + "T";
		}
		else if (vol >= b)
		{
			l = Math.round(vol / b);
			return l + "B";
		}
		else if (vol >= m)
		{
			l = Math.round(vol / m);
			return l + "M";
		}
		else if (vol >= k)
		{
			l = Math.round(vol / k);
			return l + "K";
		}
		else
		{
			l = Math.round(vol);
			return l + "";
		}

		/*  String label = "";
		  
		  long l1 = Math.round(vol / m);
		  
		  long l2 = Math.round(vol / k);
		  
		  long l3 = Math.round(vol);
		  
		  if(l1 >= 1){
			  
			  label = l1 + "M";
		  }else if(l2 >= 1){
			  
			  label = l2 + "K";
		  }else{
			  
			  label = l3 + "";
		  }
		  
		  return label;*/

	}

	public List<String> getYLabels(double start, double end, int numLabels)
	{

		if (yLabels != null)
		{

			return yLabels;
		}

		//		    List<Double> labels = new ArrayList<Double>();
		List<String> labels = new ArrayList<String>();
		/*  double[] labelParams = computeLabels(start, end, approxNumLabels);
		// when the start > end the inc will be negative so it will still work
		 int numLabels = 1 + (int) ((labelParams[1] - labelParams[0]) / labelParams[2]);
		 // we want the range to be inclusive but we don't want to blow up when
		 // looping for the case where the min and max are the same. So we loop
		 // on
		 // numLabels not on the values.
		*/

		if (isEqual(start, end))
		{

			labels.add(0 + "");

			if (!isEqual(start, 0))
			{

				labels.add(getStringYLabel(start));
			}

			return labels;
		}

		if (numLabels < 2)
		{
			numLabels = 2;
		}

		double s = start;
		double e = end;
		boolean switched = false;
		if (s > e)
		{
			switched = true;
			double tmp = s;
			s = e;
			e = tmp;
		}

		Double m = (s + e) / 2;
		labels.add(getStringYLabel(s));
		labels.add(getStringYLabel(m));
		labels.add(getStringYLabel(e));

		yLabels = labels;

		return yLabels;
	}

	public void initSize(int width, int height)
	{

		leftWidth = width / 10;
		rightWidth = width / 25;
		topHeight = height / 7;
		bottomHeight = height / 13;

	}

}
